# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=mpi4py
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=4.0.3
pkgrel=1
pkgdesc="Python bindings for the Message Passing Interface (MPI) standard (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://mpi4py.github.io/stable/'
msys2_repository_url="https://github.com/mpi4py/mpi4py"
msys2_references=(
  'purl: pkg:pypi/mpi4py'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-msmpi")
source=("https://github.com/mpi4py/mpi4py/releases/download/${pkgver}/mpi4py-${pkgver}.tar.gz")
sha256sums=('de2710d73e25e115865a3ab63d34a54b2d8608b724f761c567b6ad58dd475609')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}/"
  export MINGW_PATH=$(cygpath -m ${MINGW_PREFIX})
  sed -i 's@## mpi_dir              = /usr/local/mpi@mpi_dir              = '"${MINGW_PATH}"'@g' mpi.cfg
  sed -i 's@## mpicc                = %(mpi_dir)s/bin/mpicc@mpicc                = %(mpi_dir)s/bin/mpicc.exe@g' mpi.cfg
  sed -i 's@## mpicxx               = %(mpi_dir)s/bin/mpicxx@mpicxx               = %(mpi_dir)s/bin/mpicxx.exe@g' mpi.cfg
  sed -i 's@## include_dirs         = %(mpi_dir)s/include@include_dirs         = %(mpi_dir)s/include@g' mpi.cfg
  sed -i 's@## libraries            = mpi@libraries            = msmpi@g' mpi.cfg
  sed -i 's@## library_dirs         = %(mpi_dir)s/lib@library_dirs         = %(mpi_dir)s/lib@g' mpi.cfg
  #sed -i 's@## runtime_library_dirs = %(mpi_dir)s/lib@runtime_library_dirs = %(mpi_dir)s/bin@g' mpi.cfg
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --no-isolation
}

package() {
  cd "${srcdir}"/python-build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} --destdir="${pkgdir}" dist/*.whl
  install -Dm644 LICENSE.rst -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/python-${_realname}/
}
